package com.ajx.blog.service.impl;

import java.util.List;

import com.ajx.blog.entity.Roles;
import com.ajx.blog.mapper.RolesMapper;
import com.ajx.blog.service.api.RolesService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class RolesServiceImpl extends ServiceImpl<RolesMapper, Roles> implements RolesService {

    @Autowired
    private RolesMapper rolesMapper;

    /**
     * @param {Roles} role 角色对象
     * @return {boolean} 添加是否成功 true/false
     * 
     * @description: 添加一个角色
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean insertOneRole(Roles role) {
        if (role != null) {
            return rolesMapper.insert(role) > 0;
        } else {
            return false;
        }
    }

    /**
     * @param {Integer}   rid 角色id
     * @param {Integer[]} ids 菜单id列表
     * @return {boolean} 更新是否成功 true/false
     * 
     * @description: 更新角色对应的菜单权限
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean updateAdminMenuById(Integer rid, Integer[] ids) {
        if (rid != null) {
            rolesMapper.deleteAdminMenuById(rid);
            if (ids[0] != -1) {
                for (int i = 0; i < ids.length; i++) {
                    rolesMapper.insertAdminMenu(rid, ids[i]);
                }
            }
            return true;
        } else {
            return false;
        }
    }

    /**
     * @param {Integer} id 管理员主键id
     * @return {List<Roles>} 管理员所拥有的角色列表
     * 
     * @description: 通过id查询角色列表
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public List<Roles> selectRoleByAdmin(Integer id) {
        if (id != null) {
            QueryWrapper<Roles> qw = new QueryWrapper<>();
            qw.inSql("id", "select roles from roles_admins where admins = " + id);
            return rolesMapper.selectList(qw);
        } else {
            return null;
        }

    }

    /**
     * @param {*}
     * @return {List<Roles>} 角色列表
     * 
     * @description: 查询所有角色
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public List<Roles> selectAllRole() {
        QueryWrapper<Roles> qw = new QueryWrapper<>();
        return rolesMapper.selectList(qw);
    }

    /**
     * @param {Integer} page 当前页码数
     * @return {*} 封装的分页结果对象
     * 
     * @description: 分页查询所有角色
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public PageInfo<Roles> selectAllRoleByLimit(Integer page) {
        if (page != null) {
            QueryWrapper<Roles> qw = new QueryWrapper<>();
            PageHelper.startPage(page, 5);
            List<Roles> roleList = rolesMapper.selectList(qw);
            return new PageInfo<Roles>(roleList);
        } else {
            return null;
        }
    }

}
